package com.guicelcc.guicedemo.poi;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class DocReader {
	
	public static void readToDoc(String inReadPath,Integer number, Test2Data test2Data,String outPutPath) {
		String newFileName = outPutPath+number+"Form of Stock Purchase  Agreement-SHAO SHI NA.doc";
		File file = new File(inReadPath);
		 File wordFile = new File(newFileName);
		 nioTransferCopy(file,wordFile);
		 
		 writeDoc(newFileName,test2Data);
		 
		 
       /*try {
        	//编写压缩后文件 流
            FileOutputStream wordOut = new FileOutputStream(wordFile);
            FileInputStream fis = new FileInputStream(file);
            
            XWPFDocument xdoc = new XWPFDocument(fis);
            XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
            String doc1 = extractor.getText();
            doc1 = doc1.replaceAll("s1ss", test2Data.getS1());
            doc1 = doc1.replaceAll("s2ss", test2Data.getS2());
            doc1 = doc1.replaceAll("s3ss", test2Data.getS3());
            doc1 = doc1.replaceAll("s4ss", test2Data.getS4());
            doc1 = doc1.replaceAll("s5ss", test2Data.getS5());
            doc1 = doc1.replaceAll("s6ss", test2Data.getS6());
            doc1 = doc1.replaceAll("s7ss", test2Data.getS7());
            doc1 = doc1.replaceAll("s8ss", test2Data.getS8());
            doc1 = doc1.replaceAll("s9ss", test2Data.getS9());

            System.out.println(doc1);
            
           // XWPFDocument document = new XWPFDocument();
           
           // XWPFParagraph p1 = document.createParagraph();
           // XWPFRun r1 = p1.createRun();
            
           
            
            //xdoc.write(wordOut);
            wordOut.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }*/
	}
	
	public static void readToDoc2(String inReadPath,Integer number, Test2Data test2Data,String outPutPath,String fileEnd) {
		String newFileName = outPutPath+number+fileEnd;
		File file = new File(inReadPath);
		 File wordFile = new File(newFileName);
		 nioTransferCopy(file,wordFile);
		 
		 writeDoc(newFileName,test2Data);
	}
	
	 public static void writeDoc(String path,Test2Data test2Data) {  
         try {  
             FileInputStream in = new FileInputStream(path);  
             HWPFDocument hdt = new HWPFDocument(in);  
             Range range = hdt.getRange();  
             //读取word文本内容  
             System.out.println(range.text());  
             //替换文本内容  
             range.replaceText("s1ss",test2Data.getS1());
             range.replaceText("s2ss",test2Data.getS2());
             range.replaceText("s3ss",test2Data.getS3());
             range.replaceText("s4ss",test2Data.getS4());
             range.replaceText("s5ss",test2Data.getS5());
             range.replaceText("s6ss",test2Data.getS6());
             range.replaceText("s7ss",test2Data.getS7());
             range.replaceText("s8ss",test2Data.getS8());
             range.replaceText("s9ss",test2Data.getS9());
 
             ByteArrayOutputStream ostream = new ByteArrayOutputStream();  
             FileOutputStream out = new FileOutputStream(path);  
             hdt.write(ostream);  
             //输出字节流  
             out.write(ostream.toByteArray());  
             ostream.close();  
         } catch (IOException e) {  
             e.printStackTrace();  
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
     }
	
	/**
	      * 将源文件拷贝一份
	      * @param source
	      * @param target
	      */
	     public static void nioTransferCopy(File source, File target) {  
	         FileChannel in = null;  
	         FileChannel out = null;  
	         FileInputStream inStream = null;  
	         FileOutputStream outStream = null;  
	         try {  
	             inStream = new FileInputStream(source);  
	             outStream = new FileOutputStream(target);  
	             in = inStream.getChannel();  
	             out = outStream.getChannel();  
	             in.transferTo(0, in.size(), out);  
	         } catch (IOException e) {  
	             e.printStackTrace();  
	         }  
	     }  
	     
	     public static void writeDoc(String path,Map<String, String> map) {  
	         try {  
	             FileInputStream in = new FileInputStream(path);  
	             HWPFDocument hdt = new HWPFDocument(in);  
	             Range range = hdt.getRange();  
	             //读取word文本内容  
	             System.out.println(range.text());  
	             //替换文本内容  
	             for (Map.Entry<String,String> entry:map.entrySet()) {  
	                 range.replaceText(entry.getKey(),entry.getValue());  
	             }  
	 
	             ByteArrayOutputStream ostream = new ByteArrayOutputStream();  
	             FileOutputStream out = new FileOutputStream(path);  
	             hdt.write(ostream);  
	             //输出字节流  
	             out.write(ostream.toByteArray());  
	             ostream.close();  
	         } catch (IOException e) {  
	             e.printStackTrace();  
	         } catch (Exception e) {  
	             e.printStackTrace();  
	         }  
	     }
	
	public static void readToDoc(String inReadPath, List<Test2Data> data,String outPutPath) {
		for (int i=0;i<data.size();i++) {
			readToDoc(inReadPath, i+1, data.get(i),outPutPath);
		}
		for (int i=0;i<data.size();i++) {
		String newFileName = outPutPath+(i+1)+"Form of Stock Purchase  Agreement-SHAO SHI NA.doc";
		 File wordFile = new File(newFileName);
		Word2PDFUtil.wordPdf(wordFile.getAbsolutePath(),outPutPath);
		}
	}
	
	public static void readToDoc2(String inReadPath, List<Test2Data> data,String outPutPath,String fileEnd) {
		for (int i=0;i<data.size();i++) {
			readToDoc2(inReadPath, i+1, data.get(i),outPutPath,fileEnd);
		}
		for (int i=0;i<data.size();i++) {
		String newFileName = outPutPath+(i+1)+fileEnd;
		 File wordFile = new File(newFileName);
		Word2PDFUtil.wordPdf(wordFile.getAbsolutePath(),outPutPath);
		}
	}
}
